package org.agilelang.generator.cakephp;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IModel;
import org.agilelang.object.Model;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.AttributeVirtual;
import org.agilelang.object.attributes.Reference;

public class GModel extends Generator implements IModel {

    public GModel(String path, List<Model> listModel) {
        this.path = path;
        this.models = listModel;
        this.setTemplate2("cakephp");
        this.template = "bootstrap";
    }

    public void process() {
        int t = models.size();
        for (int i = 0; i < t; i++) {
            String model_reference = "";
            String model_atrributes_virtuals = "";
            String model_validate = "";
            Model model = models.get(i);
            int tr = model.references.size();
            for (int j = 0; j < tr; j++) {
                Reference reference = model.references.get(j);
                model_reference = model_reference
                        + "\t\t'" + reference.model_reference + "' => array(\n"
                        + "\t\t\t'className' => '" + reference.model_reference + "',\n"
                        + "\t\t\t'foreignKey' => '" + reference.foreign_key + "',\n"
                        + "\t\t\t'conditions' => '',\n"
                        + "\t\t\t'fields' => '',\n"
                        + "\t\t\t'order' => ''\n"
                        + "\t\t),\n";
            }
            tr = model.attribute_virtual.size();
            for (int j = 0; j < tr; j++) {
                AttributeVirtual virtual = model.attribute_virtual.get(j);
                String fileds = "";
                String[] list_name = virtual.value.split(",");
                for (int z = 0; z < list_name.length; z++) {
                    fileds += model.name+"."+list_name[z] + ",\"  \", ";
                }
                model_atrributes_virtuals += "'" + virtual.name + "' => 'CONCAT(" + fileds + "\"\")'\n";
            }
            List<Attribute> listattr = model.attributes;
            Collections.sort(listattr, new CustomComparator());
            int ta = listattr.size();
            for (int j = 0; j < ta; j++) {
                Attribute attr = listattr.get(j);
                String validate_attr = "\t\t'" + attr.name + "' => array(\n";
                if (attr.validate.equals("true")) {
                    switch (attr.type) {
                        case "int":
                        case "double":
                            validate_attr += "\t\t\t'rule' => 'numeric',\n";
                            break;
                        case "string":
                            validate_attr += "\t\t\t'rule' => 'alphaNumeric',\n";
                            break;
                        case "date":
                            validate_attr += "\t\t\t'rule' => 'date',\n";
                            break;
                    }
                    if (attr.nullable.equals("false")) {
                        validate_attr += "\t\t\t'required' => true,\n";
                        validate_attr += "\t\t\t'allowEmpty' => false,\n";
                    }
                    validate_attr += "\t\t\t'message' => 'Error " + attr.name + "'\n";

                }
                validate_attr += "\t\t),\n";
                model_validate += validate_attr;
            }

            String file = readStreamTpl("m_model.php.tpl");
            file = file.replace("//nameClass//", model.name);
            file = file.replace("//model_atrributes_virtuals//", model_atrributes_virtuals);
            file = file.replace("//belongsTo//", "\tvar $belongsTo = array(\n" + model_reference + "\n\t);");
            file = file.replace("//validate//", model_validate);
            createfile("model/" + model.name + ".php", path, file);
        }
    }

    public class CustomComparator implements Comparator<Attribute> {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.item.compareTo(o2.item);
        }
    }
}
